<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="所属会员" prop="memberId">
        <el-select v-model="queryParams.memberId" placeholder="请选择所属会员" clearable filterable>
          <el-option
            v-for="item in wxUserList"
            :key="item.memberId"
            :label="item.nickName"
            :value="item.memberId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="续费产品" prop="renewId">
        <el-select v-model="queryParams.renewId" placeholder="请选择所属续费产品" clearable filterable>
          <el-option
            v-for="item in renewList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          show-word-limit maxlength="50"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['funct:topup:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['funct:topup:edit']"
        >查看</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['funct:topup:remove']"
        >删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['funct:topup:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="topupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属会员" align="center" prop="memberId" :formatter="tranNickName" />
      <el-table-column label="所属续费产品" align="center" prop="renewId" :formatter="tranRenewName" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="orderAmount" />
      <el-table-column label="支付成功时间" align="center" prop="successTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.successTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报文信息" align="center" prop="respContent" :show-overflow-tooltip="true" />
      <el-table-column label="备注信息" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['funct:topup:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['funct:topup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充值记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属会员" prop="memberId">
          <el-select v-model="form.memberId" :disabled="true" placeholder="请选择所属会员" clearable filterable>
            <el-option
              v-for="item in wxUserList"
              :key="item.memberId"
              :label="item.nickName"
              :value="item.memberId"
            />
          </el-select>
<!--          <el-input v-model="form.memberId" placeholder="请输入所属会员" />-->
        </el-form-item>
        <el-form-item label="所属续费产品" prop="renewId">
          <el-select v-model="form.renewId" :disabled="true" placeholder="请选择所属续费产品" clearable filterable>
            <el-option
              v-for="item in renewList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
<!--          <el-input v-model="form.renewId" placeholder="请输入所属续费产品" />-->
        </el-form-item>
        <el-form-item label="订单号" prop="orderId">
          <el-input v-model="form.orderId" :disabled="true" show-word-limit maxlength="50" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-radio-group v-model="form.orderStatus">
            <el-radio
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="订单金额" prop="orderAmount">
          <el-input v-model="form.orderAmount" show-word-limit maxlength="12" :disabled="true" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="微信反馈报文信息">
          <el-input type="textarea" rows="3" v-model="form.respContent" show-word-limit maxlength="1000" :min-height="192"/>
        </el-form-item>
        <el-form-item label="支付成功时间" prop="successTime">
          <el-date-picker clearable
                          v-model="form.successTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择支付成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input type="textarea" rows="3" v-model="form.remark" show-word-limit maxlength="1000" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTopup, getTopup, delTopup, addTopup, updateTopup } from "@/api/funct/topup";
import { listAllWxUser } from "@/api/funct/member";
import { listAllRenew } from "@/api/funct/renew";

export default {
  name: "Topup",
  dicts: ['order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值记录表格数据
      topupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        renewId: null,
        orderId: null,
        orderStatus: null,
        orderAmount: null,
        respContent: null,
        successTime: null,
        remark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      wxUserList: [],
      renewList: []
    };
  },
  created() {
    this.getWxUserList();
    this.getRenewList();
    this.getList();
  },
  methods: {
    // 查询所有微信用户信息
    getWxUserList() {
      this.loading = true;
      listAllWxUser().then(res => {
        this.wxUserList = res.rows;
        this.loading = false;
      })
    },
    // 查询所属续费产品信息
    getRenewList() {
      this.loading = true;
      listAllRenew().then(res => {
        this.renewList = res.rows;
        this.loading = false;
      })
    },
    /** 查询充值记录列表 */
    getList() {
      this.loading = true;
      listTopup(this.queryParams).then(response => {
        this.topupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 转换用户名称
    tranNickName(row, column, cellValue) {
      if (this.wxUserList != null && this.wxUserList != undefined && this.wxUserList != undefined) {
        for (let i = 0; i < this.wxUserList.length; i++) {
          if (this.wxUserList[i].memberId == cellValue) {
            return this.wxUserList[i].nickName;
          }
        }
      }
    },
    // 转换用户名称
    tranRenewName(row, column, cellValue) {
      if (this.renewList != null && this.renewList != undefined && this.renewList != undefined) {
        for (let i = 0; i < this.renewList.length; i++) {
          if (this.renewList[i].id == cellValue) {
            return this.renewList[i].title;
          }
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        renewId: null,
        orderId: null,
        orderStatus: null,
        orderAmount: null,
        respContent: null,
        successTime: null,
        createTime: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTopup(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      return;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTopup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTopup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充值记录编号为"' + ids + '"的数据项？').then(function() {
        return delTopup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('funct/topup/export', {
        ...this.queryParams
      }, `topup_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
